package ltd.sumfsy.grain.service.impl;

import ltd.sumfsy.grain.bean.User;
import ltd.sumfsy.grain.bean.example.UserExample;
import ltd.sumfsy.grain.mapper.UserMapper;
import ltd.sumfsy.grain.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户相关服务实现类
 *
 * @author 范颂扬
 * @create 2021-05-12 21:16
 */
@Service
public class UserServiceImpl implements UserService {

    private final UserMapper userMapper;

    @Autowired
    public UserServiceImpl(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    public boolean register(User user) {
        int res = userMapper.insertSelective(user);
        return res > 0;
    }

    @Override
    public boolean logoutById(Integer id) {
        int res = userMapper.updateByPrimaryKeySelective(new User().setId(id).setDelFlag(1));
        return res > 0;
    }

    @Override
    public boolean logoutByTel(String tel) {
        UserExample example = new UserExample();
        example.createCriteria().andTelephoneEqualTo(tel);
        int res = userMapper.updateByExampleSelective(new User().setDelFlag(1), example);
        return res > 0;
    }

    @Override
    public boolean alterDataById(User user) {
        int res = userMapper.updateByPrimaryKey(user);
        return res > 0;
    }

    @Override
    public User queryById(Integer id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public User queryByTel(String tel) {
        UserExample example = new UserExample();
        example.createCriteria().andTelephoneEqualTo(tel).andDelFlagEqualTo(0);
        List<User> users = userMapper.selectByExample(example);
        if (users != null && users.size() > 0) {
            return users.get(0);
        }
        return null;
    }

    @Override
    public User queryByName(String name) {
        UserExample example = new UserExample();
        example.createCriteria().andUsernameEqualTo(name).andDelFlagEqualTo(0);
        List<User> users = userMapper.selectByExample(example);
        if (users != null && users.size() > 0) {
            return users.get(0);
        }
        return null;
    }

    @Override
    public List<User> queryAll() {
        UserExample example = new UserExample();
        example.createCriteria().andDelFlagEqualTo(0);
        return userMapper.selectByExample(example);
    }

    @Override
    public boolean isExistByUsername(String username) {
        UserExample example = new UserExample();
        example.createCriteria().andUsernameEqualTo(username).andDelFlagEqualTo(0);
        long res = userMapper.countByExample(example);
        return res > 0;
    }

    @Override
    public boolean isExistByTel(String tel) {
        UserExample example = new UserExample();
        example.createCriteria().andTelephoneEqualTo(tel).andDelFlagEqualTo(0);
        long res = userMapper.countByExample(example);
        return res > 0;
    }

    @Override
    public boolean isExistForLogin(User user) {
        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        criteria.andPasswordEqualTo(user.getPassword()).andDelFlagEqualTo(0);
        if (user.getUsername() != null) {
            criteria.andUsernameEqualTo(user.getUsername());
        } else if (user.getTelephone() != null) {
            criteria.andTelephoneEqualTo(user.getTelephone());
        }
        long res = userMapper.countByExample(example);
        return res > 0;
    }

}
